/*顶部菜单*/
(function ($) {
    $.fn.yqTopMenu = function (opts, param) {
        if (typeof opts == 'string') {
            return $.fn.yqTopMenu.methods[opts](this, param);
        }

        return this.each(function () {
            var $this = $(this);
            var opt;
            if ($this.data("yqtype") == "yqTopMenu") {
                var $uniqueID = $this.attr("ID");
                var opts_ = $.extend({}, options($this));
                destroy($this);
                $this = $("#" + $uniqueID);
                opt = $.extend(opts_, opts);
            }
            else {
                opt = $.extend({}, $.fn.yqTopMenu.defaults, opts);
                var $uniqueID = $this.attr("ID");
                if ($uniqueID === undefined || $uniqueID === null || $uniqueID == "") {
                    $uniqueID = $.getUniqueID();
                    $this.attr("ID", $uniqueID);
                }
            }

            opt.linkbutton = [];
            opt.menubutton = [];

            //变量信息
            $this.data("topmenu_opt", opt);
            $this.data("yqtype", "yqTopMenu");

            inittopmenu($this);
        });
    };

    $.fn.yqTopMenu.defaults = {
        menus: [],
        mini: false,
        beforeBindingTitle: function (menus, callback) {
            callback(menus);
        }, //标题绑定前
        afterBindingTitle: function () { }, //标题绑定后
    };

    $.fn.yqTopMenu.methods = {
        options: function (jq) {
            return options($(jq[0]));
        },
        destroy: function (jq) {
            destroy($(jq[0]));
        },
        getBtn: function (jq, id) {
            return getBtn(jq, id);
        },
        enable: function (jq, id) {
            isEdit($(jq[0]), id, true);
        },
        disable: function (jq, id) {
            isEdit($(jq[0]), id, false);
        },
        show: function (jq, id) {
            isShow($(jq[0]), id, true);
        },
        hide: function (jq, id) {
            isShow($(jq[0]), id, false);
        },
        getValue: function (jq, id) {
            return getValue($(jq[0]), id);
        },
    };

    function options($object) {
        return $object.data("topmenu_opt");
    }

    function form_load($object) {
        var opt = options($object);
        opt.afterBindingTitle.call($object);
    }

    function destroy($object) {
        var opt = options($object);
        if (opt === undefined) {
            return;
        }

        var $uniqueID = $object.attr("id");

        for (var i = 0; i < opt.menus.length; i++) {
            var para = opt.menus[i];
            if (para.type === undefined) {

            }
            else {
                var id_ = $uniqueID + "_" + para.id;
                if (para.type == "textbox") {
                    var $txt = $('#' + id_);
                    $txt.textbox("destroy");
                }
                else if (para.type == "combobox") {
                    var $ss = $('#' + id_);
                    $ss.combobox("destroy");
                }
            }
        };

        for (var i = 0; i < opt.linkbutton.length; i++) {
            opt.linkbutton[i].remove();
        }

        for (var i = 0; i < opt.menubutton.length; i++) {
            opt.menubutton[i].menubutton("destroy");
        }

        $object.removeData("topmenu_opt");
        $object.removeData("yqtype");
        $object.empty();
    }

    function inittopmenu($object) {
        var opt = options($object);

        opt.beforeBindingTitle.call($object, opt.menus, function (menus) {
            opt.menus = menus;
            initmenu_root($object, menus);
        });
    }

    function initmenu_root($object, menus) {
        var opt = options($object);
        var $uniqueID = $object.attr("id");

        var $table = $('<table></table>');
        var $tr = $('<tr></td>');
        $table.append($tr);
        $object.append($table);

        opt.linkbutton = [];
        opt.menubutton = [];
        for (var i = 0; i < menus.length; i++) {
            var $td = $('<td></td>');
            $tr.append($td);

            if (menus[i].type === undefined) {
                var $obj = $('<a href="javascript:void(0);"></a>');

                if (!(menus[i].data === undefined)) {
                    $obj.data("extdata", menus[i].data);
                }

                $td.append($obj);
                if (menus[i].text === undefined && menus[i].iconCls === undefined) {
                    $obj.addClass("pagination-btn-separator");
                }
                else {
                    if (menus[i].subs === undefined) {
                        menus[i].subs = [];
                    }

                    var i_opt = {
                        id: $uniqueID + "_" + menus[i].id,
                        plain: true,
                    };

                    if (typeof menus[i].text != "undefined" && menus[i].text.length > 0) {
                        if (menus[i].mini === undefined) {
                            menus[i].mini = opt.mini;
                        }
                        if (!menus[i].mini) {
                            i_opt.text = menus[i].text;
                        }
                        else {
                            $obj.attr("title", menus[i].text);
                        }
                    }

                    if (typeof menus[i].iconCls != "undefined" && menus[i].iconCls.length > 0) {
                        i_opt.iconCls = (menus[i].iconCls.indexOf("yqwin-btn-icon") == -1 ? "yqwin-btn-icon " + menus[i].iconCls : menus[i].iconCls);
                    }

                    if (menus[i].subs.length == 0 && menus[i].onClick) {
                        $obj.data("onClick", menus[i].onClick);
                        if (typeof menus[i].options != "undefined") {
                            $obj.data("menu-opt", menus[i].options);
                        }
                        i_opt.onClick = function () {
                            $(this).data("onClick").call(this);
                        }
                    }

                    if (menus[i].subs.length == 0) {
                        $obj.linkbutton(i_opt);
                        opt.linkbutton.push($obj);
                    }
                    else {
                        initmenu_subs($uniqueID, menus[i],
                            function (menu, $div) {
                                if (menu.subs.length > 0) {
                                    var menuid = $uniqueID + "_" + menus[i].id + "_menu";
                                    $div.attr("id", menuid);

                                    $obj.after($div);
                                    menus[i] = menu;

                                    i_opt.menu = "#" + menuid;
                                    $obj.menubutton(i_opt);

                                    opt.menubutton.push($obj);
                                }
                            });
                    }
                }
            }
            else {
                var para = $.extend({}, menus[i]);
                var id_ = $uniqueID + "_" + para.id;
                delete para.id;

                if (menus[i].type == "textbox") {
                    var $txt = $('<input id="' + id_ + '" type="text"/>');
                    $td.append($txt);
                    $txt.textbox(para);
                }
                else if (menus[i].type == "combobox") {
                    var $ss = $('<select id="' + id_ + '"/>');
                    $td.append($ss);
                    para.editable = false;
                    if (para.panelHeight === undefined) {
                        para.panelHeight = 'auto';
                    }
                    $ss.combobox(para);
                }
            }
        };

        form_load($object);
    }

    function initmenu_subs($uniqueID, menu, callback) {
        var maxChar = 0;
        for (var i = 0; i < menu.subs.length; i++) {
            var len = $.yqGetStrLen(menu.subs[i].text);
            if (len > maxChar) {
                maxChar = len;
            }
        }
        maxChar = maxChar * 10;
        if (maxChar < 150) {
            maxChar = 150;
        }

        var $div = $('<div style="width:' + (menu.width === undefined ? maxChar : menu.width) + 'px;">');
        var menu_sep = false;
        var menu_num = 0;
        for (var i = 0; i < menu.subs.length; i++) {
            if (menu.subs[i].subs === undefined) {
                menu.subs[i].subs = [];
            }

            var $obj = $('<div></div>');
            if (!(menu.subs[i].data === undefined)) {
                $obj.data("extdata", menu.subs[i].data);
            }

            if (menu.subs[i].text === undefined && menu.subs[i].iconCls === undefined) {
                if (menu_sep == false) {
                    $obj.addClass("menu-sep");
                    $div.append($obj);
                    menu_sep = true;
                }
            }
            else {
                if (menu.subs[i].subs == 0) {
                    if (typeof menu.subs[i].text != "undefined" && menu.subs[i].text.length > 0) {
                        $obj.html(menu.subs[i].text);
                    }
                    if (typeof menu.subs[i].iconCls != "undefined" && menu.subs[i].iconCls.length > 0) {
                        $obj.attr("data-options", "iconCls:'" + menu.subs[i].iconCls + "'");
                    }

                    $obj.attr("id", $uniqueID + "_" + menu.subs[i].id);

                    if (menu.subs[i].onClick) {
                        $obj.data("onClick", menu.subs[i].onClick);
                        if (typeof menu.subs[i].options != "undefined") {
                            $obj.data("menu-opt", menu.subs[i].options);
                        }
                        $obj.bind('click', function () {
                            if (!$(this).hasClass("menu-item-disabled")) {
                                $(this).data("onClick").call(this);
                            }
                        });
                    }
                }
                else {
                    var $span = $('<span>Open</span>');
                    if (typeof menu.subs[i].text != "undefined" && menu.subs[i].text.length > 0) {
                        $span.html(menu.subs[i].text);
                    }
                    if (typeof menu.subs[i].iconCls != "undefined" && menu.subs[i].iconCls.length > 0) {
                        $span.attr("data-options", (menu.subs[i].iconCls.indexOf("yqwin-btn-icon-sub") == -1 ? "yqwin-btn-icon-sub " + menu.subs[i].iconCls : menu.subs[i].iconCls));
                    }
                    $obj.append($span);
                    initmenu_subs($uniqueID, menu.subs[i],
                        function (s_menu, $s_div) {
                            if (s_menu.subs.length > 0) {
                                menu.subs[i] = s_menu;
                                $obj.append($s_div);
                            }
                        });
                }
                $div.append($obj);
                menu_sep = false;
                menu_num++;
            }
        }

        if (menu_num == 0) {
            $div.empty();
            menu.subs = [];
        }

        callback(menu, $div);
    }

    function getBtn($object, id) {
        var opt = options($object);
        var $uniqueID = $object.attr("id");
        var $btn = $("#" + $uniqueID + "_" + id);
        if ($btn.length > 0) {
            return $btn;
        }

        return null;
    }

    function isEdit($object, id, tf) {
        var opt = options($object);
        var $uniqueID = $object.attr("id");
        var $btn = $("#" + $uniqueID + "_" + id);
        if ($btn.length == 0) {
            return;
        }

        var topmenu = null;
        for (var i = 0; i < opt.menus.length; i++) {
            if (opt.menus[i].id == id) {
                topmenu = opt.menus[i];
                break;
            }
        }

        if (!(topmenu === null)) {
            if (topmenu.type === undefined) {
                if (tf) {
                    $btn.linkbutton('enable');
                } else {
                    $btn.linkbutton('disable');
                }
            }
            else if (topmenu.type == "textbox") {
                if (tf) {
                    $btn.textbox('enable');
                } else {
                    $btn.textbox('disable');
                }
            }
            else if (topmenu.type == "combobox") {
                if (tf) {
                    $btn.combobox('enable');
                } else {
                    $btn.combobox('disable');
                }
            }
        }
        else {
            var btn = getmenutop(null, opt.menus, 0, id);
            if (!(btn === null)) {
                var $menu = $("#" + $uniqueID + "_" + btn.id);
                if (!($menu === null)) {
                    if (tf) {
                        $menu.menu('enableItem', $btn[0]);
                    } else {
                        $menu.menu('disableItem', $btn[0]);
                    }
                }
            }
        }
    }

    function isShow($object, id, tf) {
        var opt = options($object);
        var $uniqueID = $object.attr("id");
        var $btn = $("#" + $uniqueID + "_" + id);
        if ($btn.length == 0) {
            return;
        }

        var topmenu = null;
        for (var i = 0; i < opt.menus.length; i++) {
            if (opt.menus[i].id == id) {
                topmenu = opt.menus[i];
                break;
            }
        }

        if (!(topmenu === null)) {
            if (tf) {
                $btn.show();
            } else {
                $btn.hide();
            }
        }
        else {
            var btn = getmenutop(null, opt.menus, 0, id);
            //console.log(btn);
            if (!(btn === null)) {
                var $menu = $("#" + $uniqueID + "_" + btn.id);
                if (!($menu === null)) {
                    if (tf) {
                        $menu.menu('enableItem', $btn[0]);
                    } else {
                        $menu.menu('disableItem', $btn[0]);
                    }
                }
            }
        }
    }

    function getValue($object, id) {
        var opt = options($object);
        var $uniqueID = $object.attr("id");
        var $btn = $("#" + $uniqueID + "_" + id);
        if ($btn.length == 0) {
            return null;
        }

        var value = null;

        var topmenu = null;
        for (var i = 0; i < opt.menus.length; i++) {
            if (opt.menus[i].id == id) {
                topmenu = opt.menus[i];
                break;
            }
        }

        if (!(topmenu === null)) {
            if (topmenu.type == "textbox") {
                value = $btn.textbox('getValue');
            }
            else if (topmenu.type == "combobox") {
                value = $btn.combobox('getValue');
            }
        }

        return value;
    }

    function getmenutop(menu, menus, level, id) {
        for (var i = 0; i < menus.length; i++) {
            if (level == 0) {
                menu = menus[i];
            }

            if (menus[i].id == id) {
                return menu;
            }

            if (menus[i].subs === undefined) {
                menus[i].subs = [];
            }

            if (menus[i].subs.length > 0) {
                return getmenutop(menu, menus[i].subs, level + 1, id);
            }
        }

        return menu;
    }

})(jQuery);